﻿// -----------------------------------------------------------------------
// <copyright file="XmlFileRank.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex3.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;
    using FacebookWrapper.ObjectModel;
    using Ex3.Logic;

    /// <summary>
    /// Concrete product
    /// </summary>
    public class XmlFileRank : XmlRepository, IFileRankRepository
    {
        private const string k_FileNameRankPhotos = @"Rank.dat";

        public XmlFileRank(User i_LoggedinUser) : base(i_LoggedinUser)
        {
        }

        public void SaveRankPhotos(List<FileRank> i_filePhotos)
        {
            string directory = this.getFileDirectory();
            this.verifyFileDirectoryExists(directory);
            string fullPath = Path.Combine(directory, k_FileNameRankPhotos);
            this.saveFile(i_filePhotos, fullPath);
        }

        public List<FileRank> LoadRankPhotos()
        {
            List<FileRank> rankPhotos = null;
            string path = Path.Combine(this.getFileDirectory(), k_FileNameRankPhotos);
            if (File.Exists(path))
            {
                using (FileStream stream = File.OpenRead(path))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<FileRank>));
                    rankPhotos = (List<FileRank>)serializer.Deserialize(stream);
                }
            }

            return rankPhotos;
        }
    }
}
